package com.gdsdxy.bookshop.service.impl;

import com.gdsdxy.bookshop.dao.UserMapper;
import com.gdsdxy.bookshop.pojo.User;
import com.gdsdxy.bookshop.service.UserService;
import com.gdsdxy.bookshop.service.ex.AdminNotFoundException;
import com.gdsdxy.bookshop.service.ex.PasswordNotMatchException;
import com.gdsdxy.bookshop.service.ex.UserExistsException;
import com.gdsdxy.bookshop.service.ex.UserNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public abstract class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 功能描述1：（管理员权限下）查看全部用户
     * 功能描述2：用户登录
     * author:吕丹
     * @Param
     * @return 返回
     */
    @Override
    public List<User> queryUserList() {
        List<User> users = userMapper.queryUserList();
        return users;
    }

    @Override
    public User login(String userName, String password) {
        // 调用userMapper的findByUsername()方法，根据参数username查询用户数据
        User result = userMapper.selectByUnameAndPwd(userName, password);
        // 判断查询结果是否为null
        if (result == null) {
            // 是：抛出UserNotFoundException异常
            throw new UserNotFoundException("用户数据不存在的错误");
        }
        User user = new User();
        // 将查询结果中的username装到新的user对象中
        user.setUsername(result.getUsername());
        user.setPassword(result.getPassword());
        // 返回新的user对象
        return user;
    }

    /**
     * 功能描述：管理员登录
     * author:李琪琪
     * @Param
     * @return 返回
     */
    @Override
    public User adminLogin(String userName, String password, Integer state) {
        User result = userMapper.selectByUnameAndPwdAndState(userName, password,state);
        if (result == null) {
            // 是：抛出UserNotFoundException异常
            throw new AdminNotFoundException("管理员数据不存在的错误");
        }
        User user = new User();
        // 将查询结果中的username装到新的user对象中
        user.setUsername(result.getUsername());
        user.setPassword(result.getPassword());
        // 返回新的user对象
        return user;
    }

    /**
     * 功能描述：管理员注册
     * author:彭惠仪
     * @Param
     * @return 返回
     */

    @Override
    public int insert(User user) {
        // 调用userMapper的findByUsername()方法，根据参数username查询用户数据
        String userName=user.getUsername();
        String password=user.getPassword();
        User result = userMapper.selectByUnameAndPwd(userName, password);
        // 判断查询结果是否为null
        if (result != null) {
            // 不是：抛出UserExistsException异常
            throw new UserExistsException("用户已存在");
        }
        user.setUsername(user.getUsername());
        user.setPassword(user.getPassword());
        user.setName(user.getName());
        user.setPhone(user.getPhone());
        user.setEmail(user.getEmail());
        int users=userMapper.insert(user);
        //userService.insert(user);
        // 将查询结果中的username装到新的user对象中
        return users;
    }

}